﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SIMS_GUI_Item
{
    public partial class GradeQuery : Form
    {
        public GradeQuery()
        {
            InitializeComponent();
        }
        SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-3CMC9MB\SQLEXPRESS;Initial Catalog=SIMS GUI Item DataBase;Integrated Security=True;");
        private void GradeQuery_Load(object sender, EventArgs e)
        {
            var sql = "select -1 GradeId,'-全部-' GradeName union select * from dbo.Grade";
            SqlCommand comm = new SqlCommand(sql, conn);
            //创建数据适配器
            SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
            //创建数据集
            DataSet ds = new DataSet();
            //向数据集填充数据
            adp.Fill(ds);

            this.cbo_Grade.DisplayMember = "GradeName"; //设置那一列给用户看
            this.cbo_Grade.ValueMember = "GradeId";     //设置关键数据列
            this.cbo_Grade.DataSource = ds.Tables[0];   //设置Combobox的数据源

            //是否允许自动创建列
            this.dgv_GradeQuery.AutoGenerateColumns = false;
            //不允许出现空行
            this.dgv_GradeQuery.AllowUserToAddRows = false;
            //整行选中
            this.dgv_GradeQuery.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //是否允许多选
            this.dgv_GradeQuery.MultiSelect = false;
            this.dgv_GradeQuery.RowTemplate.Height = 60;
        }

        /// <summary>
        /// 搜索按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Search_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select a.RealName,a.Sex,a.IdentityCard,b.GradeId from UserInformation a 
                        inner join Grade b on a.GradeId=b.GradeId where 1=1");//联合查询
            if (Convert.ToInt32(this.cbo_Grade.SelectedValue) != -1)
                sb.AppendFormat(" and a.GradeId={0}", this.cbo_Grade.SelectedValue);//根据年级选择
            SqlDataAdapter adp = new SqlDataAdapter(sb.ToString(), conn);
            DataSet ds = new DataSet();
            adp.Fill(ds);//填满数据
            this.dgv_GradeQuery.DataSource = ds.Tables[0];
        }
    }
}
